﻿using System.Collections.Generic;

using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

using YuoTools;

public class YuoAnimaCon : MonoBehaviour
{
    Dictionary<string, AnimationClipPlayable> playables = new Dictionary<string, AnimationClipPlayable>();
    [HideInInspector]
    public AnimaItems Animas;
    [ReadOnly]
    public AnimaItem NowAnima;
    Animator _animator;
    PlayableGraph graph;
    AnimationPlayableOutput playableOutput;
    protected virtual void Awake()
    {
        Destroy(GetComponent<Animator>());
        _animator = gameObject.AddComponent<Animator>();
        Animas = GetComponent<AnimaItems>();
        graph = PlayableGraph.Create($"[{gameObject.name}] YuoAnimator");
        // 创建一个Output节点，类型是Animation，名字是Anima，目标对象是物体上的Animator组件
        playableOutput = AnimationPlayableOutput.Create(graph, "Anima", _animator);
        ListToDic();
    }
    private void OnDestroy()
    {
        graph.Destroy();
    }
    public void Play(string animationName)
    {
        if (!Animas.animaItems.ContainsKey(animationName))
        {
            Debug.LogWarning($"[{gameObject.name}] 上不存在 [{animationName}]动画");
            return;
        }
        NowAnima = Animas.animaItems[animationName];
        NowAnima.playable = playables[animationName];
        NowAnima.playable.SetTime(0);
        // 将playable连接到output
        playableOutput.SetSourcePlayable(NowAnima.playable);
        // 播放这个graph
        graph.Play();
    }
    private void Update()
    {
        //NowAnima.playable.GetTime().Log();
        NowAnima.playable.SetSpeed(Time.timeScale * NowAnima.speed);
    }
    void ListToDic()
    {
        Animas.animaItems.Clear();
        foreach (var item in Animas._animaItems)
        {
            // 创建一个动画剪辑Playable，将clip传入进去
            item.playable = AnimationClipPlayable.Create(graph, item.clip);
            if (Animas.animaItems.ContainsKey(item.name))
            {
                Animas.animaItems[item.name] = item;
                playables[item.name] = item.playable;
            }
            else
            {
                playables.Add(item.name, item.playable);
                Animas.animaItems.Add(item.name, item);
            }
            item.length = item.clip.length;
            item.IsLoop = item.clip.isLooping;
        }
    }
    public void UpdateClip(string name, AnimationClip clip)
    {
        Animas.animaItems[name].clip = clip;
        Animas.animaItems[name].length = clip.length;
        Animas.animaItems[name].playable = AnimationClipPlayable.Create(graph, clip);
    }
    [System.Serializable]
    public class AnimaItem
    {
        public string name = "null";
        [SerializeField]
        private float _speed = 1;
        public float speed { get => _speed; set { _speed = value; playable.SetSpeed(value); } }
        public AnimationClip clip;
        [ReadOnly]
        public bool IsLoop;
        [ReadOnly]
        public float length;
        [HideInInspector]
        public AnimationClipPlayable playable;
    }
}
